package jogs.core;
import java.sql.*;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import jogs.core.GameServer;
//import jogs.Player;


public class JogsDB implements DB
{
	private static final Logger logger = Logger.getLogger(JogsDB.class.getName());  /** The {@link Logger} for this class. */
	public static Handler handler = new ConsoleHandler();
	static
    {
    	logger.addHandler(handler);
    }
	private static final String classname = "com.mysql.jdbc.Driver";
	private String url;
	private Connection conn;
	//private String tableNum;
	
	//Constructors
	//====================================================
	public JogsDB()
	{
		url = "jdbc:mysql://localhost/gameserver?user=dactry&password=cool";
		//tableNum = "default";
		setCon(url);
	}
	
	public void setCon(String nurl)
	{
		try
		{
			Class.forName(classname);
			url = nurl;
			conn = DriverManager.getConnection(url);
		}
		catch (Exception e)
		{
			logger.log(Level.WARNING, "Problem connecting to the DataBase\n" + e.toString());
		}
	}
	
	public boolean update(String insert)
	{
		try
		{
			Statement stmt = conn.createStatement();
            int rows = stmt.executeUpdate(insert);
			if(rows < 1)
			{
				logger.log(Level.WARNING, "Table Was not Updated");
				return false;
			}
			return true;
			
		}
		catch(Exception e)
		{
			logger.log(Level.WARNING, "Problem inserting into table\n" + insert + "\n" + e.toString());
			return false;
		}
	}
	
	public boolean updateStatus(int status, String tableNum)
	{
		String update = "update gameTable set status=" + status + " where tableNumber=" + tableNum;
		return update(update);
	}

	
	public boolean addTable(String gameType, int numPlayers, String tableNum)
	{
		String insert = "insert into gameTable values (" + tableNum + ",'" + gameType + "', 1," + numPlayers + ",0," + 1 + ");";
		return update(insert);
	}
	
	public boolean addPlayer(String player, int occu, String tableNum)
	{
		//int occu = 1;
		//get occupied seats and num total seats and compare, also create a general Query method
		
		String insert = "insert into playerTable values ('" + player + "', " + tableNum + ");";
		
		if(update(insert))
		{
			//occu += 1;
			String update = "update gameTable set occupiedSeats=" + occu + " where tableNumber=" + tableNum + ";";
			return update(update);
			 
		}
		else
		{
			return false;
		}
	}
	
	public void close()
	{
		try
		{
			//these 2 lines are only there for the debugging.
			//this.update("delete from gameTable where tableNumber=" + tableNum + ";");
			//this.update("delete from playerTable where tableNumber=" + tableNum + ";");
			conn.close();
		}
		catch(Exception e)
		{
			logger.log(Level.WARNING, "Problem closing connection\n" + e.toString());
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
